package com.xmtlxx.oms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.oms.domain.TaskDtl;
import com.xmtlxx.oms.domain.bo.TaskBo;
import com.xmtlxx.oms.domain.bo.TaskDtlBo;
import com.xmtlxx.oms.domain.vo.TaskDtlExportVo;
import com.xmtlxx.oms.domain.vo.TaskDtlVo;
import com.xmtlxx.oms.domain.vo.TaskVo;
import com.xmtlxx.oms.mapper.TaskDtlMapper;
import com.xmtlxx.oms.mapper.TaskMapper;
import com.xmtlxx.oms.service.ITaskDtlService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.system.domain.bo.SysOssBo;
import com.xmtlxx.system.mapper.SysOssMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 出入库订单明细Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-12
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TaskDtlServiceImpl implements ITaskDtlService {

    private final TaskDtlMapper baseMapper;

    private final SysOssMapper sysOssMapper;

    private final TaskMapper taskMapper;

    /**
     * 查询出入库订单明细
     *
     * @param id 出入库订单明细主键
     * @return 出入库订单明细
     */
    @Override
    public TaskDtlVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectTaskDtlVoByID(id);
    }

    /**
     * 查询出入库订单明细列表
     *
     * @param bo 出入库订单明细
     * @return 出入库订单明细
     */
    @Override
    public TableDataInfo<TaskDtlVo> queryPageList(TaskDtlBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<TaskDtl> lqw = buildQueryWrapper(bo);
       // Page<TaskDtlVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<TaskDtlVo> result = baseMapper.selectPageTaskDtlList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询出入库订单明细列表
     *
     * @param bo 出入库订单明细
     * @return 出入库订单明细
     */
    @Override
    public List<TaskDtlVo> queryList(TaskDtlBo bo) {
       // LambdaQueryWrapper<TaskDtl> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        List<TaskDtlVo> dtlVos = baseMapper.selectTaskDtlList(bo);
        if(dtlVos != null && dtlVos.size() > 0){
            for (TaskDtlVo dtlVo:dtlVos){
                SysOssBo ossBo = new SysOssBo();
                ossBo.setObjid(dtlVo.getPno());
                dtlVo.setOss(sysOssMapper.selectOssList(ossBo));

                TaskVo taskVo = taskMapper.selectVoById(dtlVo.getTaskId());
                if(taskVo != null && !StringUtils.isEmpty(taskVo.getType()) && !taskVo.getType().equals("0")){
                    if(StringUtils.isEmpty(dtlVo.getStoreId())){
                        dtlVo.setStoreId(taskVo.getStoreId());
                    }
                }
            }
        }

        return dtlVos;
    }

    private LambdaQueryWrapper<TaskDtl> buildQueryWrapper(TaskDtlBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TaskDtl> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTaskId()), TaskDtl::getTaskId, bo.getTaskId());
        lqw.eq(StringUtils.isNotBlank(bo.getPno()), TaskDtl::getPno, bo.getPno());
        lqw.like(StringUtils.isNotBlank(bo.getName()), TaskDtl::getName, bo.getName());
        lqw.eq(bo.getDeptId() != null, TaskDtl::getDeptId, bo.getDeptId());
        lqw.eq(bo.getLocalId() != null, TaskDtl::getLocalId, bo.getLocalId());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), TaskDtl::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增出入库订单明细
     *
     * @param bo 出入库订单明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(TaskDtlBo bo) {
        TaskDtl add = BeanUtil.toBean(bo, TaskDtl.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改出入库订单明细
     *
     * @param bo 出入库订单明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(TaskDtlBo bo) {
        TaskDtl update = BeanUtil.toBean(bo, TaskDtl.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TaskDtl entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除出入库订单明细
     *
     * @param ids 需要删除的出入库订单明细主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<TaskDtlVo> taskDtlDeptList() {
        return baseMapper.taskDtlDeptList();
    }

    @Override
    public List<TaskDtlVo> getDtlPropertyClassify(Long deptId) {
        return baseMapper.getDtlPropertyClassify(deptId);
    }

    @Override
    public List<TaskDtlExportVo> queryExportList(TaskBo bo) {
        return baseMapper.queryExportList(bo);
    }
}
